//
//  EditorPersonalInformation.m
//  OnionHome
//
//  Created by fengjie on 2019/9/14.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "EditorPersonalInformation.h"
#import "PersonalInformationCell.h"
#import "ChangNickNameController.h"
#import "ChangIntroduceController.h"
#import "ChooseCityController.h"
#import "ChangeTimerController.h"
#import "CustomCameraController.h"
#import "AddressPickerController.h"
#import "ChangGenderController.h"

@interface EditorPersonalInformation ()
/**listview管理*/
@property (nonatomic,strong)UITableViewTool*listViewTool;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataSoure;
@end

@implementation EditorPersonalInformation

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑资料";
    [self.view addSubview:self.listViewTool.listView];
    [self.listViewTool.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndNotClearColor];
}
#pragma mark --懒加载

-(UITableViewTool*)listViewTool{
    if(!_listViewTool){
        kWeakSelf(weakSelf);
        _listViewTool=[[UITableViewTool alloc] initWithFrame:CGRectZero style:UITableViewStylePlain registerCellClassName:NSStringFromClass([PersonalInformationCell class]) identifier:NSStringFromClass([PersonalInformationCell class]) dataArray:self.dataSoure delegate:self dataSourceBlock:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            PersonalInformationCell * cell = (PersonalInformationCell *)TempCell;
            cell.indexPath = indexPath;
            cell.dic = (NSDictionary *)TempData;
        } selectCell:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            if (indexPath.row == 0) {
                [self changeUserIcon];
            }
            if (indexPath.row == 1) {
                [weakSelf changeNickNameAction];
            }
            if (indexPath.row == 2) {
                [self changeGender];
            }
            if (indexPath.row == 3) {
                [self changeTimer];
            }
            if (indexPath.row == 4) {
                [weakSelf onClickChooseCity];
            }
            if (indexPath.row == 5) {
                [weakSelf changIntroduceAction];
            }
        }];
    }
    return _listViewTool;
}

/// 修改昵称
-(void)changeNickNameAction{
    NSDictionary * itemDic = self.dataSoure[1];
    
    PresentationObject * object = [PresentationObject sharedPresentationObject];
//    object.alpha_bgView = 1;
    object.subViewFrame =CGRectMake(0, KScreenHeight-166, KScreenWidth, 166);
    ChangNickNameController * changVC = [ChangNickNameController new];
    changVC.defutName = itemDic[@"rightName"];
    [object showPresentingViewController:self PresentedViewController:changVC];
    kWeakSelf(weakSelf);
    changVC.tapBlock = ^(NSString *text) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic setValue:@"昵称" forKey:@"leftName"];
        [dic setValue:text forKey:@"rightName"];
        weakSelf.dataSoure[1] = dic;
        
        [weakSelf.listViewTool reloadData:weakSelf.dataSoure];
    };
    
}

/// 修改简介
-(void)changIntroduceAction{
        NSDictionary * itemDic = self.dataSoure[5];
        PresentationObject * object = [PresentationObject sharedPresentationObject];
//        object.alpha_bgView = 1;
        object.subViewFrame =CGRectMake(0, KScreenHeight-166, KScreenWidth, 166);
        ChangIntroduceController * changVC = [ChangIntroduceController new];
        changVC.defutName = itemDic[@"rightName"];
        [object showPresentingViewController:self PresentedViewController:changVC];
        kWeakSelf(weakSelf);
        changVC.tapBlock = ^(NSString *text) {
            NSMutableDictionary * dic = [NSMutableDictionary dictionary];
            [dic setValue:@"简介" forKey:@"leftName"];
            [dic setValue:text forKey:@"rightName"];
            weakSelf.dataSoure[5] = dic;
            
            [weakSelf.listViewTool reloadData:weakSelf.dataSoure];
        };
}

/// 修改生日
-(void)changeTimer{
    PresentationObject * object = [PresentationObject sharedPresentationObject];
//    object.alpha_bgView = 1;
//    object.subViewFrame =CGRectMake(0, KScreenHeight-166, KScreenWidth, 166);
    ChangeTimerController * changVC = [ChangeTimerController new];
    [object showPresentingViewController:self PresentedViewController:changVC];
    kWeakSelf(weakSelf);
    changVC.returnBlock = ^(NSString * _Nonnull timer) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic setValue:@"简介" forKey:@"leftName"];
        [dic setValue:timer forKey:@"rightName"];
        weakSelf.dataSoure[3] = dic;
        
        [weakSelf.listViewTool reloadData:weakSelf.dataSoure];

    };
}
/// 修改地址
- (void)onClickChooseCity {
    AddressPickerController * vc = [AddressPickerController new];
    PresentationObject * object = [PresentationObject sharedPresentationObject];
    object.subViewFrame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    [object showPresentingViewController:self PresentedViewController:vc];
    kWeakSelf(weakSelf);
    vc.backBlock = ^(NSString *text) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic setValue:@"地区" forKey:@"leftName"];
        [dic setValue:text forKey:@"rightName"];
        weakSelf.dataSoure[4] = dic;
        [weakSelf.listViewTool reloadData:weakSelf.dataSoure];
    };
}
/**
 修改性别
 */
-(void)changeGender{
    
    FJAlertController * alerCon = [FJAlertController alertControllerWithTitle:@"" message:@"选择性别" preferredStyle:UIAlertControllerStyleActionSheet];
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:@"性别" forKey:@"leftName"];

    kWeakSelf(weakSelf);
    FJAlertAction * action = [FJAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [dic setValue:@"男" forKey:@"rightName"];
        weakSelf.dataSoure[2] = dic;
        [weakSelf.listViewTool reloadData:weakSelf.dataSoure];

    }];
    FJAlertAction * action1 = [FJAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [dic setValue:@"女" forKey:@"rightName"];
        weakSelf.dataSoure[2] = dic;
        [weakSelf.listViewTool reloadData:weakSelf.dataSoure];
    }];
    
    FJAlertAction * action2 = [FJAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];

    action.textColor = UIColorFromRGB(0x009966);
    action1.textColor = UIColorFromRGB(0x009966);
    action2.textColor = UIColorFromRGB(0x009966);


    [alerCon addAction:action];
    [alerCon addAction:action1];
    [alerCon addAction:action2];


    [self presentViewController:alerCon animated:YES completion:^{

    }];
}

/// 修改用户头像
-(void)changeUserIcon{
    FJAlertController * alerCon = [FJAlertController alertControllerWithTitle:@"" message:@"选择上传方式" preferredStyle:UIAlertControllerStyleActionSheet];
    kWeakSelf(weakSelf);
    FJAlertAction * action = [FJAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf selectTheCamera];
    }];
    FJAlertAction * action1 = [FJAlertAction actionWithTitle:@"从相册上传" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf selectPhotoAlbum];
    }];
    FJAlertAction * action2 = [FJAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];

    
    action.textColor = UIColorFromRGB(0x009966);
    action1.textColor = UIColorFromRGB(0x009966);
    action2.textColor = UIColorFromRGB(0x009966);

    
    [alerCon addAction:action];
    [alerCon addAction:action1];
    [alerCon addAction:action2];

    [self presentViewController:alerCon animated:YES completion:nil];

}

/// 选择相册
-(void)selectPhotoAlbum{
    kWeakSelf(weakSelf);
//    [PhotosManager showPhotosManager:self withMaxImageCount:1 withAlbumArray:^(NSMutableArray<PhotoModel *> *albumArray) {
//        DLog(@"%@", albumArray);
       
//
//    }];
    [PhotosManager showPhotosManager:self withMaxImageCount:1 withAlbumArray:^(NSMutableArray<PhotoModel *> * _Nonnull albumArray) {
      if (albumArray.count == 0) {
           return ;
       }
       PhotoModel * model = albumArray.lastObject;
       NSMutableDictionary * dic = [NSMutableDictionary dictionary];
       [dic setValue:@"简介" forKey:@"leftName"];
       [dic setValue:@"" forKey:@"rightName"];
       [dic setValue:model.highDefinitionImage forKey:@"rightImage"];
       weakSelf.dataSoure[0] = dic;
       [weakSelf.listViewTool reloadData:weakSelf.dataSoure];
    } withCameraImage:^(UIImage * _Nonnull cameraImage) {
        
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic setValue:@"简介" forKey:@"leftName"];
        [dic setValue:@"" forKey:@"rightName"];
        [dic setValue:cameraImage forKey:@"rightImage"];
        weakSelf.dataSoure[0] = dic;
        [weakSelf.listViewTool reloadData:weakSelf.dataSoure];

    }];

}

/// 选择相机拍照
-(void)selectTheCamera{
    CustomCameraController * vc = [CustomCameraController new];
    [self presentViewController:[[BasseNavigationController alloc]initWithRootViewController:vc] animated:YES completion:nil];
    kWeakSelf(weakSelf);
    vc.rentunBlock = ^(UIImage * _Nonnull image) {
        DLog(@"%@",image);
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic setValue:@"简介" forKey:@"leftName"];
        [dic setValue:@"" forKey:@"rightName"];
        [dic setValue:[self imageByCroppingWithImage:image] forKey:@"rightImage"];
        weakSelf.dataSoure[0] = dic;
        [weakSelf.listViewTool reloadData:weakSelf.dataSoure];

    };

}

//剪裁图片
-(UIImage *)imageByCroppingWithImage:(UIImage*)myImage{
    //    CGFloat ratioPic =  0.5f;//宽高比1比1
    
    CGFloat myWidth = myImage.size.width*myImage.scale;
    //image.size.width乘以缩放比才是真正的尺寸。
    //图像的实际的尺寸(像素)等于image.size乘以image.scale
    //    CGFloat myHeight = ceil(myWidth*ratioPic);
    CGFloat myHeight = myImage.size.height*myImage.scale*0.5;
    
    CGRect rect = CGRectMake(0,0,myWidth,myHeight);
    
    CGImageRef imageRef = myImage.CGImage;
    CGImageRef imagePartRef=CGImageCreateWithImageInRect(imageRef,rect);
    UIImage * cropImage=[UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    
    return cropImage;
}
-(NSMutableArray *)dataSoure{
    if (!_dataSoure) {
        NSArray * array = @[@{@"leftName":@"头像",@"rightName":@"",@"rightImage":IMAGE_NAMED(@"user2")}
                            ,@{@"leftName":@"昵称",@"rightName":@"我是一个购物狂",@"rightImage":IMAGE_NAMED(@"user2")}
                            ,@{@"leftName":@"性别",@"rightName":@"男",@"rightImage":IMAGE_NAMED(@"user2")}
                            ,@{@"leftName":@"生日",@"rightName":@"2000-03-18",@"rightImage":IMAGE_NAMED(@"user2")}
                            ,@{@"leftName":@"地区",@"rightName":@"广东省深圳市",@"rightImage":IMAGE_NAMED(@"user2")}
                               ,@{@"leftName":@"简介",@"rightName":@"爱生活 爱美食 爱自己",@"rightImage":IMAGE_NAMED(@"user2")}];
        _dataSoure =[NSMutableArray array];
        [_dataSoure addObjectsFromArray:array];
    }
    return _dataSoure;
}
@end
